#!/usr/bin/Rscript
## This R script prints out a table of a given evaluation method from csv files
#  VERSION 1.1
#  Gabi, NICTA CRL, 2015
#
# Usage: 
#	MeasureTable.R eval files output	
#	eval: evaluation method
#	
# Example: 

##### MUC
# ./MeasureTable.R --eval=exact_micro_F1Measure --files=results/MUC7/baseline_MUC7_option_1_overall.csv,results/MUC7/baseline_MUC7_option_2_overall.csv,results/MUC7/baseline_MUC7_option_3_overall.csv,results/MUC7/baseline_MUC7_option_5_overall.csv,results/MUC7/shallow_models_MUC7_1_NONE_2_2_overall.csv,results/MUC7/shallow_models_MUC7_2_NONE_2_2_overall.csv --output=tables/MicroExactMUC.csv

# ./MeasureTable.R --eval=newDigit_micro_F1Measure --files=results/MUC7/baseline_MUC7_option_1_overall.csv,results/MUC7/baseline_MUC7_option_2_overall.csv,results/MUC7/baseline_MUC7_option_3_overall.csv,results/MUC7/baseline_MUC7_option_5_overall.csv,results/MUC7/shallow_models_MUC7_1_NONE_2_2_overall.csv,results/MUC7/shallow_models_MUC7_2_NONE_2_2_overall.csv --output=tables/MicroNewDigitMUC.csv

##### I2b2
# ./MeasureTable.R --eval=newDigit_micro_F1Measure --files=results/i2b2/baseline_I2B2_option_1_overall.csv,results/i2b2/baseline_I2B2_option_2_overall.csv,results/i2b2/baseline_I2B2_option_3_overall.csv,results/i2b2/baseline_I2B2_option_5_overall.csv,results/i2b2/shallow_models_I2B2_2_NONE_2_2_overall.csv,results/i2b2/shallow_models_2_overall.csv --output=tables/MicroNewDigitI2b2.csv

# ./MeasureTable.R --eval=exact_micro_F1Measure --files=results/i2b2/baseline_I2B2_option_1_overall.csv,results/i2b2/baseline_I2B2_option_2_overall.csv,results/i2b2/baseline_I2B2_option_3_overall.csv,results/i2b2/baseline_I2B2_option_5_overall.csv,results/i2b2/shallow_models_I2B2_2_NONE_2_2_overall.csv,results/i2b2/shallow_models_2_overall.csv --output=tables/MicroExactI2b2.csv

# ./MeasureTable.R --eval=is-a_micro_F1Measure --files=results/i2b2/baseline_I2B2_option_1_overall.csv,results/i2b2/baseline_I2B2_option_2_overall.csv,results/i2b2/baseline_I2B2_option_3_overall.csv,results/i2b2/baseline_I2B2_option_5_overall.csv,results/i2b2/shallow_models_I2B2_2_NONE_2_2_overall.csv,results/i2b2/shallow_models_2_overall.csv --output=tables/Micro-is-aI2b2.csv

##### BBN
# ./MeasureTable.R --eval=newDigit_micro_F1Measure --files=results/BBN/baseline_BBN_option_1_overall.csv,results/BBN/baseline_BBN_option_2_overall.csv,results/BBN/baseline_BBN_option_3_overall_generated.csv,results/BBN/baseline_BBN_option_5_overall_generated.csv,results/BBN/shallow_models_BBN_2_NONE_2_2_overall_generated.csv,results/BBN/shallow_models_BBN_1_NONE_2_2_overall_generated.csv  --output=tables/MicroNewDigitBBN.csv

# ./MeasureTable.R --eval=exact_micro_F1Measure --files=results/BBN/baseline_BBN_option_1_overall.csv,results/BBN/baseline_BBN_option_2_overall.csv,results/BBN/baseline_BBN_option_3_overall_generated.csv,results/BBN/baseline_BBN_option_5_overall_generated.csv,results/BBN/shallow_models_BBN_2_NONE_2_2_overall_generated.csv,results/BBN/shallow_models_BBN_1_NONE_2_2_overall_generated.csv  --output=tables/MicroExactBBN.csv --output=tables/MicroExactBBN.csv

# ./MeasureTable.R --eval=is-a_micro_F1Measure --files=results/BBN/baseline_BBN_option_1_overall.csv --output=tables/Micro-is-aBBN.csv 
 
#,results/BBN/baseline_BBN_option_5_overall_generated.csv,results/BBN/shallow_models_BBN_2_NONE_2_2_overall_generated.csv,results/BBN/shallow_models_BBN_1_NONE_2_2_overall_generated.csv --output=tables/Micro-is-aBBN.csv 



## Collect arguments
args<-commandArgs(TRUE)

## Default setting when no arguments passed
if(length(args) < 1) {
  args <- c("--help")
}
 
## Help section
if("--help" %in% args) {
  cat("
      The R Script
 
      Arguments:
      --eval=chunks_F1Measure          - evaluation method
      --files=file1,file2,file3...     - files to be listed
      --output=output.csv              - output file name
      --help                           - print this text
 
      Example:
      ./MeasureTable.R --eval=exact_micro_F1Measure --files=results/conll2bbn_brownv4000.txt_4.csv,results/conll2muc7_brownv4000.txt_9.csv,results/ner_combined_conll_muc7_0.csv,results/ner_combined_conll_muc7_9.csv,results/ner_train_test_bbn_4.csv,results/ner_train_test_muc7_9.csv,results/newNER_results_stanford_bbn_4.csv,results/newNER_results_stanford_muc7_4.csv,results/newNER_results_stanford_muc7_9.csv --output=tables/F1Measure.csv \n\n")
 
  q(save="no")
}

parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
argsL <- as.list(as.character(argsDF$V2))
names(argsL) <- argsDF$V1

files <- strsplit(argsL$files,",")[[1]]
eval=toString(argsL$eval)

temp = data.matrix(read.delim(file = files[1], header = TRUE, sep = "\t"))

out <- as.data.frame(temp[,"label"])

colnames(out)[1] <- "label"
decimal <- function(x, k) format(round(x, k), nsmall=k)

for(f in 1:length(files)) {

	temp = data.matrix(read.delim(file = files[f], header = TRUE, sep = "\t"))


	out <- cbind(out,as.data.frame(decimal(as.numeric(temp[,eval]),4)))

	fileName <- basename(files[f])
	fileName <- gsub("_","+",fileName)
	print(fileName)
	colnames(out)[f+1] <- substr(fileName, 0, regexpr("+[^+]*$", fileName)-2)
}
print(eval)
print(out)
write.csv(out, file = argsL$output, eol = "\n", quote=FALSE, row.names = FALSE)



